package com.yogee.topseller.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.yogee.topseller.R;
import com.yogee.topseller.activity.AllMechanismActivity;
import com.yogee.topseller.activity.ProductDetailsActivity;
import com.yogee.topseller.activity.login.model.bean.InterestLabelBean;
import com.yogee.topseller.utils.LogUtils;
import com.yogee.topseller.view.FlowLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/6/21.
 */

public class InterestLabelNewAdapter extends RecyclerView.Adapter<InterestLabelNewAdapter.LabelViewHolder> {
    //上下文
    Activity mContext;
    private int TYPE_ITEM = 0, TYPE_FOOT = 1;
    //标签数据
    List<InterestLabelBean.ListBean> mLabelDatas,allLabelDatas,mSelectDatas;
    //已选中的标签
    public ArrayList<TextView> mSelectView = new ArrayList<>();

    private InterestLabelBean interestLabelBean;

    private ArrayList<InterestLabelBean.ListBean.LabelListBean> mSelectBean = new ArrayList<>();

    private List<String> selectStr = new ArrayList<>();
    private int current_position=-1;

    public InterestLabelNewAdapter(Activity context) {
        this.mContext = context;
    }

    public void setCity(InterestLabelBean interestLabelBean){

        this.interestLabelBean = interestLabelBean;
    }


    public void addData(List<InterestLabelBean.ListBean> mLabelDatas) {
        this.mLabelDatas = new ArrayList<>();
        this.allLabelDatas = new ArrayList<>();
        this.mLabelDatas = mLabelDatas;
        this.allLabelDatas = mLabelDatas;
        this.mSelectDatas = new ArrayList<>();

        if(allLabelDatas!=null&&allLabelDatas.size()>0){
            for(int i = 0 ;i<allLabelDatas.size();i++){
                InterestLabelBean.ListBean listbean = new InterestLabelBean.ListBean();
                listbean.setType(allLabelDatas.get(i).getType());
                mSelectDatas.add(listbean);
            }
        }
        notifyDataSetChanged();
    }

    public ArrayList<InterestLabelBean.ListBean.LabelListBean> getmSelectBean() {
        return mSelectBean;
    }

    public List<String> getmSelectStr(){
        return selectStr;
    }

    public List<InterestLabelBean.ListBean> getmSelectDatas(){
        return mSelectDatas;
    }
    public void setSelectItem(int position,List<InterestLabelBean.ListBean.LabelListBean> labelListBean){
        mLabelDatas.get(position).setLabelList(labelListBean);
        current_position = position;
        notifyItemChanged(position);

    }

    @Override
    public void onBindViewHolder(final LabelViewHolder holder, final int position) {

        if(current_position>0){
            if(position==current_position){

                holder.mLabelData.cleanTag();
                ArrayList<TextView> tvList = holder.mLabelData.setDataDefaultSelect(mLabelDatas.get(position).getLabelList());

                for(int i = 0;i<tvList.size();i++){
                    TextView textView = tvList.get(i);
                    if(!mSelectView.contains(textView)){
                        mSelectView.add(textView);
                        if(!mSelectBean.contains(mLabelDatas.get(position).getLabelList().get(i)))
                            mSelectBean.add(mLabelDatas.get(position).getLabelList().get(i));
                        if(!selectStr.contains(mLabelDatas.get(position).getLabelList().get(i).getName())){
                            selectStr.add(mLabelDatas.get(position).getLabelList().get(i).getName());
                        }
                        if(!mSelectDatas.get(position).getLabelList().contains(mLabelDatas.get(position).getLabelList().get(i))){
                            mSelectDatas.get(position).getLabelList().add(mLabelDatas.get(position).getLabelList().get(i));
                        }
                    }else{
//                        if(!mSelectBean.contains(mLabelDatas.get(current_position).getLabelList().get(i))){
//                            mSelectBean.add(mLabelDatas.get(current_position).getLabelList().get(i));
//                        }

                    }
                }
            }else{
                holder.mLabelData.setData(mLabelDatas.get(position).getLabelList());
            }
        }else{
            holder.mLabelData.setData(mLabelDatas.get(position).getLabelList());
        }

        holder.mLabelType.setText(mLabelDatas.get(position).getType());

        holder.mLabelData.setOnTagClickListener(new FlowLayout.OnTagClickListener() {
            @Override
            public void TagClick(InterestLabelBean.ListBean.LabelListBean bean, TextView textView) {
//                mSelectBean.add(bean);
                if(selectStr.contains(bean.getName())){
                    selectStr.remove(bean.getName());
                }else{
                    selectStr.add(bean.getName());
                }
                if (mSelectBean.contains(bean)) {
                    mSelectBean.remove(bean);
                }else{
                    mSelectBean.add(bean);
                }
                if(mSelectDatas.get(position).getLabelList().contains(bean)){
                    mSelectDatas.get(position).getLabelList().remove(bean);
                }else{
                    mSelectDatas.get(position).getLabelList().add(bean);
                }

//                selectStr.add(bean.getName());
                textView.setTextColor(mContext.getResources().getColor(R.color.select_color));
                textView.setBackgroundResource(R.drawable.corens_100_style_select);
                Drawable dra = ContextCompat.getDrawable(mContext,R.drawable.select);
                dra.setBounds(8,0,0,0);
                textView.setCompoundDrawablesWithIntrinsicBounds(ContextCompat.getDrawable(mContext,R.drawable.select),null,null,null);

                if (!mSelectView.contains(textView)) {
                    mSelectView.add(textView);

                } else {
                    textView.setBackgroundResource(R.drawable.corens_100_style_unselect);
                    textView.setTextColor(Color.parseColor("#4a4a4a"));
                    textView.setCompoundDrawablesWithIntrinsicBounds(null,null,null,null);
                    mSelectView.remove(textView);
                }
            }
        });
        if(mLabelDatas.get(position).getType().equals("地区")||mLabelDatas.get(position).getType().equals("发行机构")){
            holder.tv_more.setVisibility(View.VISIBLE);
        }else{
            holder.tv_more.setVisibility(View.GONE);
        }

        holder.tv_more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Bundle bundle = new Bundle();

                bundle.putSerializable("datas",allLabelDatas.get(position));
                bundle.putSerializable("citys",interestLabelBean);
                bundle.putSerializable("select",mSelectBean);
                mContext.startActivityForResult(new Intent(mContext, AllMechanismActivity.class).putExtras(bundle).putExtra("position",position)
                        ,0);
            }
        });
    }

    @Override
    public LabelViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

//        if (viewType == TYPE_FOOT) {
//            View view = LayoutInflater.from(mContext).inflate(R.layout.item_interest_label_new_two, parent, false);
//            return new FootHolder(view);
//        } else { //item_ranking_new
//            View view = LayoutInflater.from(mContext).inflate(R.layout.item_interest_label_new, parent, false);
//            return new LabelViewHolder(view);
//        }
//        item_interest_label
        LabelViewHolder holder = new LabelViewHolder(LayoutInflater.from(
                mContext).inflate(R.layout.item_interest_label_new, parent,
                false));
        return holder;
    }

    @Override
    public int getItemCount() {
        return mLabelDatas == null ? 0 : mLabelDatas.size();
    }

    class LabelViewHolder extends RecyclerView.ViewHolder {

        TextView mLabelType,tv_more;
        FlowLayout mLabelData;


        public LabelViewHolder(View view) {
            super(view);
            mLabelType = (TextView) view.findViewById(R.id.tv_label_title);
            mLabelData = (FlowLayout) view.findViewById(R.id.fl_label_data);
            tv_more = (TextView) view.findViewById(R.id.tv_more);
        }
    }

    class FootHolder extends RecyclerView.ViewHolder {

        public FootHolder(View itemView) {
            super(itemView);
        }
    }
}
